#!/bin/bash

# Crosslight linux build script. This will download Crosslight's components and
# build them for your local platform. It has potential for more configuration.
# Requires libssl-dev, libxml2-dev, and possibly others.

MAJOR=1
MINOR=4
FIX=0
COMMIT=`hg log -l 1 | grep changeset | awk -F: '{print $3}'`
VERSION="$MAJOR.$MINOR.$FIX"
FULLVERSION="$VERSION-$COMMIT"

run()
{
    echo "Running command:"
    echo $1
    $1
    echo
}

FORCE_BUILD="no"
HOST=`sh config.guess`
TARGET=`sh config.guess`

usage(){
	echo "$0: Build Crosslight."
	echo "	F	Force build even if the project is up to date."
	echo "	H	Specify the automake host. Defaults to $HOST (from config.guess)."
	echo "	T	Specify the automake target. Defaults to $TARGET (from config.guess)."
	echo "		H and T are only useful if you have a cross-compiling environment configured."
	echo "	h	Display this help and exit."
}

while getopts "FH:T:h" opt ; do
	case $opt in
		F) FORCE_BUILD="yes";;
		H) HOST=$OPTARG;;
		T) TARGET=$OPTARG;;
		h) usage; exit;
	esac
done

build(){
	export PKG=$1  #Package name
	export VER=$2  #Package version
	export URL=$3  #URL path to download from
	export EXT=$4  #Extension on download
	CONF=$5 #Any extra options to ./configure
	HOST=$6 #Host target.

	URL=$(echo $URL | sed "s/PKG/$PKG/;s/VER/$VER/;s/EXT/$EXT/")

	# No need to redownload, only do this once.
	FB=$FORCE_BUILD
	FILE="$ROOTDIR/tars/$PKG$VER$EXT"
	if [ ! -e "$FILE" ] ; then
		wget -O "$FILE"  "$URL"
	fi

	if [ ! -d "$ROOTDIR/$PKG" ] ; then
		mkdir -p "$ROOTDIR/$PKG"
		tar -C "$ROOTDIR/$PKG/" -xvf "$FILE"
		FB="yes";
	fi

	if [ "x$FB" = "xyes" ] ; then
		cd "$ROOTDIR/$PKG/$PKG$VER/"
		./configure --enable-static --prefix="$ROOTDIR/crosslight" $CONF #--host=$HOST --target=$TARGET
		make
		make install
	fi
}

ROOTDIR=`pwd`

echo "In $ROOTDIR"

echo "Gathering min build scripts"
tar -czf "$ROOTDIR/crosslight-$VERSION-linux-min.tar.gz" ../linux

#Prepare the working directories we'll need
mkdir -p "$ROOTDIR/tars"
mkdir -p "$ROOTDIR/crosslight/bin/lib"

echo "Building from $HOST to $TARGET"

#Build the lighttpd process itself. Want openssl, need to set appropriate directories. Throwing away documentation.
build 	"lighttpd" "-1.4.31" "http://download.lighttpd.net/lighttpd/releases-1.4.x/PKGVEREXT" ".tar.gz" "--with-openssl --with-openssl-lib=/usr/lib --sbindir=$ROOTDIR/crosslight/bin/ --libdir=$ROOTDIR/crosslight/bin/lib/ --docdir=/dev/null --mandir=/dev/null" $HOST $TARGET
						#Change if you have openssl somewhere else, or don't want ssl ever.
					 	#Even with SSL, you'll need to generate a cert. See ./etc/configssl.sh

${MYSQL_LIBS:=" --with-mysqli=mysqlnd"}
${GD_LIBS:=" --with-gd --with-jpeg-dir=$ROOTDIR/crosslight/ --with-png-dir=$ROOTDIR/crosslight --with-freetype --with-freetype-dir=/usr/lib "} #--with-zlib-dir=$ROOTDIR/crosslight"}
build "php" "-5.4.0" "http://www.php.net/get/PKGVEREXT/from/us2.php.net/mirror" ".tar.gz" "--with-openssl --without-pear --with-curl --enable-soap --enable-sockets --with-tidy $GD_LIBS $MYSQL_LIBS" $HOST $TARGET

#Everything's built
cd "$ROOTDIR"

#Clean up everything we don't need, but can't turn off in the ./configure scripts.
BIN="$ROOTDIR/crosslight/bin"
SBIN="$ROOTDIR/crosslight/sbin"
rm -rf $ROOTDIR/crosslight/{lib,man,share,include,php}
rm -rf $BIN/{pear,peardev,pecl,phar,phar.phar,php,php-config,phpize} $SBIN "$ROOTDIR/crosslight/etc/pear.conf" #Don't need any of these.

#Package the whole thing up.
tar -czf "$ROOTDIR/crosslight-$VERSION-$TARGET.tar.gz" "crosslight"

sh test.sh
